package com.boc.langxiang.ui.activity.adm;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.boc.langxiang.Constant;
import com.boc.langxiang.R;
import com.boc.langxiang.contract.adm.AdmSearchContract;
import com.boc.langxiang.entity.request.AdmSearchRequest;
import com.boc.langxiang.entity.response.AdmEntity;
import com.boc.langxiang.presenter.adm.AdmSearchPresenter;
import com.boc.langxiang.ui.activity.BaseActivity;
import com.boc.langxiang.ui.adapter.adm.AdmAdapter;
import com.boc.langxiang.util.KeyBoardUtil;
import com.boc.langxiang.util.T;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.next.tagview.TagCloudView;

public class AdmSearchActivity extends BaseActivity implements TagCloudView.OnTagClickListener,
        AdmSearchContract.View, SwipeRefreshLayout.OnRefreshListener,
        BaseQuickAdapter.RequestLoadMoreListener {

    @BindView(R.id.close)
    ImageView mClose;
    @BindView(R.id.search)
    EditText mSearch;
    @BindView(R.id.recylerview)
    RecyclerView mRecylerview;
    @BindView(R.id.tag_view)
    TagCloudView mTagView;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout mSwipeLayout;
    @BindView(R.id.hot_search)
    TextView mHotSearch;
    private AdmSearchContract.Presenter mPresenter;
    private AdmSearchRequest mAdmSearchRequest;
    private AdmAdapter mAdapter;
    private int mPage = 1;
    private boolean mIsOnRefresh;
    private boolean mIsLoadmore;
    private List<String> mTags;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_adm_search);
        ButterKnife.bind(this);

        mPresenter = new AdmSearchPresenter(this);
        initSearchView();
        initView();

        initListView();

    }

    private void initListView() {
        mSwipeLayout.setOnRefreshListener(this);
        mSwipeLayout.setColorSchemeColors(Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW);
        mRecylerview.setHasFixedSize(true);
        mRecylerview.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));

        initAdapter();
    }

    private void initAdapter() {
        mAdapter = new AdmAdapter(mContext, null);
        View emptyView = LayoutInflater.from(mContext).inflate(R.layout.view_empty, (ViewGroup) mRecylerview.getParent(), false);
        mAdapter.setEmptyView(emptyView);
        mAdapter.openLoadAnimation();
        //控制itemview动画是否只在第一次显示
        mAdapter.isFirstOnly(false);
        mAdapter.setOnLoadMoreListener(this);
        mAdapter.openLoadMore(Constant.PAGE_SIZE, true);
        mRecylerview.setAdapter(mAdapter);
        mAdapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(mContext, AdmSimpleActivity.class);
                AdmEntity item = mAdapter.getItem(position);
                intent.putExtra("designerId", item.getId());
                startActivity(intent);
            }
        });
    }

    private void initView() {
        mSwipeLayout.setVisibility(View.GONE);
        mTagView.setVisibility(View.VISIBLE);

        mTags = new ArrayList<>();
        mPresenter.hotWord();

        mTagView.setOnTagClickListener(this);
    }

    private void initSearchView() {
        mSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    KeyBoardUtil.closeKeybord(AdmSearchActivity.this);
                    mPage = 1;
                    admSearch(mSearch.getText().toString());
                    mSearch.setText("");
                    return true;
                }
                return false;
            }
        });
    }

    @Override
    public void onTagClick(int position) {
        String s = mTags.get(position);
        mPage = 1;
        admSearch(s);
    }

    private void admSearch(String keyword) {
        if (mAdmSearchRequest == null) {
            mAdmSearchRequest = new AdmSearchRequest();
        }
        mAdmSearchRequest.page = mPage;
        mAdmSearchRequest.keyword = keyword;
        mPresenter.admSearch(mAdmSearchRequest);
    }

    @OnClick({R.id.close})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.close:
                finish();
                break;
        }
    }

    @Override
    public void showLoading() {
        showProgressDialog();
    }

    @Override
    public void hideLoading() {
        hideDialog();
    }

    @Override
    public void onError(String code, String msg) {
        mIsLoadmore = false;
        mAdapter.notifyDataChangedAfterLoadMore(false);
        mIsOnRefresh = false;
        mSwipeLayout.setRefreshing(false);
        T.showShort(mContext, msg);
    }

    @Override
    public void admSearchSuccess(List<AdmEntity> res) {
        if (mIsLoadmore) {
            mIsLoadmore = false;
            if (res == null) {
                return;
            }
            if (res.size() < Constant.PAGE_SIZE) {
                mAdapter.notifyDataChangedAfterLoadMore(res, false);
                loadDone();
            } else {
                mAdapter.notifyDataChangedAfterLoadMore(res, true);
            }

        } else {
            mIsOnRefresh = false;
            mSwipeLayout.setRefreshing(false);
            if (res == null) {
                return;
            }
            mSwipeLayout.setVisibility(View.VISIBLE);
            mTagView.setVisibility(View.GONE);
            mHotSearch.setVisibility(View.GONE);
            mAdapter.setNewData(res);
        }

    }

    @Override
    public void showHotWordLoading() {
        showProgressDialog("获取热词...");
    }

    @Override
    public void hotWordSuccess(ArrayList<String> content) {
        mTags.clear();
        mTags.addAll(content);
        mTagView.setTags(content);
    }

    private void loadDone() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.view_load_done, (ViewGroup) mRecylerview.getParent(), false);
        mAdapter.addFooterView(view);
    }


    @Override
    public void onRefresh() {
        if (mIsOnRefresh) {
            return;
        }
        if (mAdmSearchRequest == null) {
            T.showShort(mContext, "请输入关键词进行搜索!");
            mSwipeLayout.setRefreshing(false);
            return;
        }
        mAdapter.openLoadMore(Constant.PAGE_SIZE, true);
        mIsOnRefresh = true;
        mPage = 1;
        mAdmSearchRequest.page = mPage;
        mPresenter.admSearch(mAdmSearchRequest);
    }

    @Override
    public void onLoadMoreRequested() {
        mRecylerview.post(new Runnable() {
            @Override
            public void run() {
                if (mIsLoadmore) {
                    return;
                }
                mIsLoadmore = true;
                mPage++;
                mAdmSearchRequest.page = mPage;
                mPresenter.admSearch(mAdmSearchRequest);

            }
        });
    }
}
